package LexPortal.Entity;

import javax.servlet.http.*;
import java.sql.*;

public class TestSqliteServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3182581135018803010L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		try {
			Class.forName("org.sqlite.JDBC");
		} catch (Exception cnf) {
			System.out.println("ClassNotFoundException: " + cnf.getMessage());
		}
		Connection conn = null;
		Statement stat = null;
		ResultSet rs = null;
		try {
			conn = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\zhuyix\\NewWS\\LexPortal\\WebContent\\test.db");

			stat = conn.createStatement();
			stat.executeUpdate("drop table if exists people;");
			stat.executeUpdate("create table people (name, occupation);");
			PreparedStatement prep = conn
					.prepareStatement("insert into people values (?, ?);");

			prep.setString(1, "Gandhi");
			prep.setString(2, "politics");
			prep.addBatch();
			prep.setString(1, "Turing");
			prep.setString(2, "computers");
			prep.addBatch();
			prep.setString(1, "Wittgenstein");
			prep.setString(2, "smartypants");
			prep.addBatch();

			conn.setAutoCommit(false);
			prep.executeBatch();
			conn.setAutoCommit(true);

			rs = stat.executeQuery("select * from people;");
			while (rs.next()) {
				System.out.println("name = " + rs.getString("name"));
				System.out.println("job = " + rs.getString("occupation"));
			}
			rs.close();
			conn.close();
		} catch (SQLException ex) {
			// handle any errors
			System.out.println("SQLException: " + ex.getMessage());
			System.out.println("SQLState: " + ex.getSQLState());
			System.out.println("VendorError: " + ex.getErrorCode());
		} finally {
			// it is a good idea to release
			// resources in a finally{} block
			// in reverse-order of their creation
			// if they are no-longer needed
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException sqlEx) {
				} // ignore
				rs = null;
			}
			if (stat != null) {
				try {
					stat.close();
				} catch (SQLException sqlEx) {
				} // ignore
				stat = null;
			}
		}
	}
}
